<template>
<div>
  <top-list-admin :activeIndex="0+''"></top-list-admin>
  <el-button plain type="primary" @click="researchEdit(0)" style="margin-left: 20px;margin-top: 20px">添加课题</el-button>
  <el-table
    :data="dataList"
    border
    stripe
    v-loading="dataListLoading"
    style="width: 100%; margin-top: 20px;">
    <el-table-column prop="subjectCode" header-align="center" align="center" label="课题编码"></el-table-column>
    <el-table-column prop="researchName" header-align="center" align="center" label="课题名称"></el-table-column>
    <el-table-column prop="researchSimpleName" header-align="center" align="center" label="课题简称"></el-table-column>
    <el-table-column prop="piName" header-align="center" align="center" label="课题PI"></el-table-column>
    <el-table-column header-align="center" align="center" label="访视阶段">
      <template slot-scope="{row}">
        <el-select  placeholder="点击查看" value="">
          <el-option v-for="item in row.vpInfo" :label=processvpinfo(item) :key="item.code" :value="item.code"></el-option>
        </el-select>
      </template>
    </el-table-column>
    <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
      <template slot-scope="scope">
        <el-button type="text" size="small" @click="researchEdit(scope.row.subjectPkId)">编辑</el-button>
        <el-button type="text" size="small" @click="researchDelete(scope.row.id,scope.row.researchName)">删除</el-button>
      </template>userPost
    </el-table-column>
  </el-table>
  <el-pagination
    @size-change="sizeChangeHandle"
    @current-change="currentChangeHandle"
    :current-page="pageIndex"
    :page-sizes="[10, 20, 50, 100]"
    :page-size="pageSize"
    :total="totalPage"
    layout="total, sizes, prev, pager, next, jumper"
    style="margin-top: 20px;"
  >
  </el-pagination>
  <research-update v-if="updateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></research-update>
</div>

</template>

<script>
import TopListAdmin from '@/components/TopListAdmin'
import research from '../../api/research'
import myUtils from '../../api/myUtils'
import researchUpdate from './research-update.vue'
import device from '../../api/device'
export default {
  components: {
    researchUpdate,
    TopListAdmin
  },
  data() {
    return {
      dataList: [
        {
          id:0,
        }
      ],
      updateVisible:false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLOADING: false
    }
  },
  created() {
    this.isadmin()
  },
  methods: {
    isadmin()
    {
      let role = myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 8)
      {
        this.$message.error("您不是系统管理员");
      }
      else {
        this.getDataList();
      }
    },
    getDataList()
    {
      this.dataListLoading = true;
      this.dataListLoading = false;
      research.researchAllSearch(this.pageIndex,this.pageSize).then(response=>{
        this.dataList = response.data.records;
        this.totalPage = response.data.total;
        for(var i=0;i<this.dataList.length;i++){
          this.dataList[i].vpInfo = eval(this.dataList[i].vpInfo);
        }
        // console.log(this.dataList)
      });

    },
    processvpinfo(item)
    {
      return item.code+" "+item.name;
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    researchEdit(id)
    {
      // console.log(id)
      let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 8) //总管理员才能操作
      {
        this.$message.error("您不是系统管理员");
        return;
      }
      this.updateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
      // console.log(this.updateVisible)
    },

    researchDelete(id,name)
    {
      let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 8) //总管理员才能操作
      {
        this.$message.error("您不是系统管理员");
        return;
      }
      this.$confirm(
        `确定对[课题名为：${name}]进行删除操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(()=>{
        research.deleteResearch(id).then(( response ) => {
          // console.log(response)
          if (response.code  === '000000') {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 700,
              onClose: () => {
                this.getDataList()
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      })
    }
  }
}
</script>
